﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Sanctuary.Core.Animation
{
    public class PanAnimation : AnimationBase
    {
        private PanType animationMoveType;
        private double distance;
        public PanAnimation(PanType moveType, double moveDistance, TimeSpan duration)
        {
            distance = moveDistance;
            animationMoveType = moveType;
            Duration = duration;
        }


        public PanAnimation(PanType moveType, double moveDistance)
        {
            distance = moveDistance;
            animationMoveType = moveType;
            Duration = TimeSpan.FromSeconds(1);
        }

        protected override Storyboard CreateStoryboard()
        {
            var storyboard = new Storyboard();
            var moveAnimation = new DoubleAnimation();
            switch (animationMoveType)
            {
                case PanType.LeftToRight:
                case PanType.RightToLeft:
                    Storyboard.SetTargetProperty(moveAnimation, new PropertyPath("(FrameworkElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.X)"));
                    break;
                case PanType.TopToBottom:
                case PanType.BottomToTop:
                    Storyboard.SetTargetProperty(moveAnimation, new PropertyPath("(FrameworkElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.Y)"));
                    break;
                default:
                    break;
            }


            storyboard.Children.Add(moveAnimation);

            return storyboard;
        }

        protected override void ApplyValues(Storyboard storyboard)
        {
            if (storyboard == null)

                throw new ArgumentNullException("storyboard");

            var moveAnimation = storyboard.Children[0] as DoubleAnimation;

            switch (animationMoveType)
            {
                case PanType.LeftToRight:
                case PanType.TopToBottom:
                    moveAnimation.From = distance;
                    moveAnimation.To = 0;
                    break;
                case PanType.RightToLeft:
                case PanType.BottomToTop:
                    moveAnimation.From = 0;
                    moveAnimation.To = distance;
                    break;
                default:
                    break;
            }

            moveAnimation.FillBehavior = FillBehavior.HoldEnd;
            moveAnimation.Duration = Duration;
        }
    }
}
